package com.hengxinguotong.zhihuichengjian.ui.building;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.AualityTestAddPicAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.BuildFileAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Document;
import com.hengxinguotong.zhihuichengjian.bean.FileBean;
import com.hengxinguotong.zhihuichengjian.bean.Build;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;

/**
 * 质量检查
 * Created by liux on 2017/3/7.
 */

public class BuildTestActivity extends BaseActivity implements AualityTestAddPicAdapter.OnRecyclerViewClickListener, BuildFileAdapter.OnClickListener{
    @BindView(R.id.tv_title_name)
    HXTextView tvTitleName;
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.order_no_tv)
    HXTextView orderNoTv;
    @BindView(R.id.order_no_rl)
    RelativeLayout orderNoRl;
    @BindView(R.id.inspect_date_tv)
    HXTextView inspectDateTv;
    @BindView(R.id.reply_date_tv)
    HXTextView replyDateTv;
    @BindView(R.id.remind_date_tv)
    HXTextView remindDateTv;
    @BindView(R.id.contacts_tv)
    HXTextView contactsTv;
    @BindView(R.id.person_number)
    HXTextView personNumber;
    @BindView(R.id.notice_tv)
    HXTextView noticeTv;
    @BindView(R.id.test_detail_etv)
    HXTextView testDetailEtv;
    @BindView(R.id.no_data_tv)
    HXTextView noDataTv;
    @BindView(R.id.pic_rv)
    RecyclerView picRv;
    @BindView(R.id.file_rv)
    RecyclerView fileRv;
    @BindView(R.id.main_sl)
    ScrollView mainSl;

    private AualityTestAddPicAdapter aualityTestAddPicAdapter;
    private List<String> paths;
    private BuildFileAdapter fileAdapter;
    private List<FileBean> fileList;
    private Build build;
    private int type = Constants.LIVE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type", Constants.LIVE);
        setContentView(R.layout.activity_buildtest);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        build = (Build) getIntent().getSerializableExtra("build");
        if (build != null) {
            orderNoTv.setText(build.getOrderNo());
            inspectDateTv.setText(build.getInspectDate());
            replyDateTv.setText(build.getDeadlineReplyDate());
            remindDateTv.setText(build.getWarningReminderDate());
            contactsTv.setText(build.getContactName());
            getUserList(build.getId());//获取告知人列表
            testDetailEtv.setText(build.getItems());
            switch (build.getCorrectiveStatus()){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
            if((build.getPicList() == null || build.getPicList().size() == 0) && (build.getFileList() == null || build.getFileList().size() == 0)){
                noDataTv.setVisibility(View.VISIBLE);
            }else {
                noDataTv.setVisibility(View.GONE);
                if(build.getFileList() != null && build.getFileList().size() > 0){
                    fileRv.setLayoutManager(getVerticalLinearLayoutManager());
                    fileList = build.getFileList();
                    fileAdapter = new BuildFileAdapter(this, fileList);
                    fileAdapter.setOnClickListener(this);
                    fileRv.setAdapter(fileAdapter);
                }
                if (build.getPicList() != null && build.getPicList().size() > 0) {
                    picRv.setLayoutManager(getHorizontalLinearLayoutManager());
                    paths = build.getPicList();
                    aualityTestAddPicAdapter = new AualityTestAddPicAdapter(this, paths, this, false);
                    picRv.setAdapter(aualityTestAddPicAdapter);
                }
            }
        }

        switch (type){
            case Constants.LIVE:
                tvTitleName.setText("任务下发详情");
                break;
        }
    }

    private LinearLayoutManager getHorizontalLinearLayoutManager() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        return linearLayoutManager;
    }

    private LinearLayoutManager getVerticalLinearLayoutManager() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return linearLayoutManager;
    }

    @OnClick({R.id.iv_back, R.id.contacts_tv})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.contacts_tv:
                break;
        }
    }

    @Override
    public void onItemClick(View v, int position) {
        Intent intent;
        if (paths.get(position).equals(Utils.ADDPIC)) {
            List<String> tempStrings = new ArrayList<String>();
            tempStrings.addAll(paths);
            tempStrings.remove(tempStrings.size() - 1);
            MultiImageSelector.create()
                    .showCamera(true) // show camera or not. true by default_grey
                    .count(9) // max select image size, 9 by default_grey. used width #.multi()
                    .multi()
                    .origin((ArrayList<String>) tempStrings)
                    .start(this, 1);
        } else {
            if (build != null) {
                TaskBigImgActivity.startActivity(BuildTestActivity.this, (ArrayList<String>) paths, position);
            } else {
                paths.remove(position);
                aualityTestAddPicAdapter.notifyDataSetChanged();
            }
        }
    }

    private void getUserList(String inspectionId) {
        Utils.requestData(null, this, "/build/queryNotifyByInspectionId/" + SPUtil.getString(this, "uuid") + "?inspectionId=" + inspectionId, null, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        int status = object.getInt("status");
                        String msg = object.getString("msg");
                        if(status == 1){
                            String value = object.getString("value");
                            noticeTv.setText(value);
                            if(Utils.isEmpty(value)){
                                personNumber.setText("共" + 0 + "人");
                            }else {
                                String[] users = value.split(",");
                                personNumber.setText("共" + users.length + "人");
                            }
                        }else {
                            showToast(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    @Override
    public void onClick(Object o) {
        FileBean fileBean = (FileBean) o;
        if(fileBean == null){
            return;
        }
        Document document = new Document();
        document.setName(fileBean.getName());
        document.setPath(fileBean.getPath());
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String url = document.getPath().replace("type=2", "type=1");
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            final ComponentName componentName = intent.resolveActivity(getPackageManager());
            // 打印Log   ComponentName到底是什么
            Log.e("wl", "componentName = " + componentName.getClassName());
            startActivity(Intent.createChooser(intent, "请选择浏览器"));
        } else {
            showToast("没有匹配的程序");
        }
    }
}
